/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tamnhat.entity;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import telsoft.admin.util.AppServer;
import telsoft.sql.Database;

/**
 *
 * @author Administrator
 */
public class Category implements Serializable {

    private String id;
    private String code;
    private String name;
    private String parent_id;

    public Category() {
    }

    public Category(String name) {
        this.name = name;
    }

    public Category(String id, String code, String name, String parent_id) {
        this.id = id;
        this.code = code;
        this.name = name;
        this.parent_id = parent_id;
    }

    @Override
    public String toString() {
        return name;
    }    
    
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getParent_id() {
        return parent_id;
    }

    public void setParent_id(String parent_id) {
        this.parent_id = parent_id;
    }

    public static List<Category> allCate(String condition) throws Exception {
        List<Category> list = new ArrayList<Category>();
        Connection cn = null;
        Statement st = null;
        ResultSet rs = null;
        String strSQL = " select * from md_categories "
                + " " + condition
                + " order by category_id";
        try {
            cn = AppServer.getConnection();
            st = cn.createStatement();
            rs = st.executeQuery(strSQL);
            while (rs.next()) {
                Category c = new Category();
                c.setId(rs.getString(1));
                c.setCode(rs.getString(2));
                c.setName(rs.getString(3));
                c.setParent_id(rs.getString(4));
                list.add(c);
            }
            Database.closeObject(rs);
            Database.closeObject(st);
        } finally {
            Database.closeObject(cn);
        }
        return list;
    }

    public static boolean checkCateId(String id) throws Exception {
        List<Category> list = allCate(" where category_id = " + id + "");
        if (list.size() > 0) {
            return true;
        }
        return false;
    }

    public String getIdByName() throws Exception {
        Connection cn = null;
        Statement st = null;
        ResultSet rs = null;
        String kq = "";
        if(getName() == null){
            return  kq;
        }
        String strSQL = " select * from md_categories where upper(name) = '" + getName().toUpperCase() + "' ";
        
        try {
            cn = AppServer.getConnection();
            st = cn.createStatement();
            rs = st.executeQuery(strSQL);
            if (rs.next()) {
                kq = rs.getString(1);
            }            
            return kq;
        } finally {
            Database.closeObject(rs);
            Database.closeObject(st);
            Database.closeObject(cn);
        }
    }
    public static String getIdByName(String name) throws Exception {
        Connection cn = null;
        Statement st = null;
        ResultSet rs = null;
        String kq = "";
        if(name == null){
            return  kq;
        }
        String strSQL = " select * from md_categories where upper(name) = '" + name.toUpperCase() + "' ";
        
        try {
            cn = AppServer.getConnection();
            st = cn.createStatement();
            rs = st.executeQuery(strSQL);
            if (rs.next()) {
                kq = rs.getString(1);
            }            
            return kq;
        } finally {
            Database.closeObject(rs);
            Database.closeObject(st);
            Database.closeObject(cn);
        }
    }

    public boolean insert() throws Exception {
        Connection cn = null;
        PreparedStatement pst = null;
        String strSQL = "";
        String check = "1";
        try {
            cn = AppServer.getConnection();
            setId(Database.getSequenceValue(cn, "seq_common"));
            if (getParent_id() == null || getParent_id().equalsIgnoreCase("")) {
                strSQL = " insert into md_categories (category_id,code,name) values(?,?,?) ";
                check = "1";
            } else {
                strSQL = " insert into md_categories values(?,?,?,?) ";
                check = "2";
            }
            pst = cn.prepareStatement(strSQL);

            pst.setInt(1, Integer.parseInt(getId()));
            pst.setString(2, getCode());
            pst.setString(3, getName());
            if (check.equals("2")) {
                pst.setInt(4, Integer.parseInt(getParent_id()));
            }
            return pst.executeUpdate() > 0;
        } finally {
            Database.closeObject(pst);
            Database.closeObject(cn);
        }
    }

    public boolean remove() throws Exception {
        Connection cn = null;
        PreparedStatement stmt = null;
        String strSQL = " delete from md_categories where category_id = " + getId() + " ";
        try {
            cn = AppServer.getConnection();
            stmt = cn.prepareStatement(strSQL);
            return stmt.executeUpdate() > 0;
        } finally {
            Database.closeObject(stmt);
            Database.closeObject(cn);
        }
    }

    public boolean update() throws Exception {
        Connection cn = null;
        PreparedStatement pst = null;
        String strSQL = "";
        String check = "1";
        try {
            cn = AppServer.getConnection();
            if (getParent_id() == null || getParent_id().equalsIgnoreCase("")) {
                strSQL = " update md_categories"
                        + " set code = ?,name = ? "
                        + " where category_id = ? ";
                check = "1";
            } else {
                strSQL = " update md_categories"
                        + " set code = ?,name = ?,parent_category_id = ? "
                        + " where category_id = ? ";
                check = "2";
            }
            pst = cn.prepareStatement(strSQL);
            
            pst.setString(1, getCode());
            pst.setString(2, getName());            
            if(check.equals("1")){
                pst.setInt(3, Integer.parseInt(getId()));
            }
            else {
                pst.setInt(3, Integer.parseInt(getParent_id()));
                pst.setInt(4, Integer.parseInt(getId()));
            }
            
            return pst.executeUpdate() > 0;
        } finally {
            Database.closeObject(pst);
            Database.closeObject(cn);
        }
    }
}
